﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;

namespace Glossary
{
    public static class IOTextFile
    {
        /// <summary>
        /// Ansvar: Martin Raldholm
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        public static CreatedTest ReadCreatedTestFromTextFile(string path)
        {
            List<string> stringsFromFile = new List<string>();

            using (StreamReader sr = new StreamReader(path, Encoding.Unicode))
            {
                string line;
                while ((line = sr.ReadLine()) != null)
                {
                    stringsFromFile.Add(line);
                }
            }

            CreatedTest createdTest = new CreatedTest(int.Parse(stringsFromFile[3]));

            if (CreatedTest.TryParse(stringsFromFile.ToArray(), createdTest))
            {
                return createdTest;
            }

            throw new FormatException(string.Format("Filen hade ogiltig format."));
        }

        /// <summary>
        /// Ansvar: Vikiet Chung
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        public static FilledTest ReadFilledTestFromTextFile(string path)
        {
            List<string> stringsFromFile = new List<string>();
            using (StreamReader sr = new StreamReader(path, Encoding.Unicode))
            {
                string line;
                while ((line = sr.ReadLine()) != null)
                {
                    stringsFromFile.Add(line);
                }
            }
            FilledTest filledTest = new FilledTest(int.Parse(stringsFromFile[3]));
            if (FilledTest.TryParse(stringsFromFile.ToArray(), filledTest))
            {
                return filledTest;
            }
            throw new FormatException(string.Format("Filen hade ogiltig format."));
        }

        /// <summary>
        /// Ansvar: Vikiet Chung
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        public static CorrectedTest ReadCorrectedTestFromTextFile(string path)
        {
            List<string> stringsFromFile = new List<string>();
            using (StreamReader sr = new StreamReader(path, Encoding.Unicode))
            {
                string line;
                while ((line = sr.ReadLine()) != null)
                {
                    stringsFromFile.Add(line);
                }
            }
            CorrectedTest correctedTest = new CorrectedTest(int.Parse(stringsFromFile[3]));

            if (CorrectedTest.TryParse(stringsFromFile.ToArray(), correctedTest))
            {
                return correctedTest;
            }
            throw new FormatException(string.Format("Filen hade ogiltig format."));
        }

        /// <summary>
        /// Ansvar: Vikiet Chung
        /// </summary>
        /// <param name="fileName"></param>
        /// <param name="createdTest"></param>
        public static void WriteCreatedTestToTextFile(string fileName, CreatedTest createdTest)
        {
            using (StreamWriter sw = new StreamWriter(fileName, false, Encoding.Unicode))
            {
                sw.WriteLine(createdTest);
            }
        }

        /// <summary>
        /// Ansvar Martin Raldholm
        /// </summary>
        /// <param name="path"></param>
        /// <param name="filledTest"></param>
        /// 
        public static void WriteFilledTestToTextFile(string fileName, FilledTest filledTest)
        {
            try
            {
                using (StreamWriter sw = new StreamWriter(fileName, false, Encoding.Unicode))
                {
                    sw.WriteLine(filledTest);
                }
            }
            catch (Exception)
            {
                throw;
            }
        }

        /// <summary>
        /// Ansvar: Vikiet Chung
        /// </summary>
        /// <param name="fileName"></param>
        /// <param name="correctedTest"></param>
        public static void WriteCorrectedTestToTextFile(string fileName, CorrectedTest correctedTest)
        {
            using (StreamWriter sw = new StreamWriter(fileName, false, Encoding.Unicode))
            {
                sw.WriteLine(correctedTest);
            }
        }

        /// <summary>
        /// Anvar: Patryk Pedersen
        /// </summary>
        /// <param name="fileName"></param>
        /// <param name="correctedTest"></param>
        public static void WriteCorrectedTestToHTMLFile(string fileName, CorrectedTest correctedTest)
        {
            using (StreamWriter sw = new StreamWriter(fileName, false, Encoding.Unicode))
            {
                sw.WriteLine(correctedTest.ToHtmlString());
            }
        }

        /// <summary>
        /// Ansvar: Kim Nielsen
        /// ReadGlossaryFromFile läser in ord från txt-fil och lägger till dem i WordLibrary
        /// </summary>
        /// <param name="wordLibrary"></param>
        /// <param name="fileName"></param>
        public static void ReadGlossaryFromFile(ref WordLibrary wordLibrary, string fileName)
        {
            Language language;
            Word word;

            using (StreamReader sr = new StreamReader(fileName, Encoding.Unicode))
            {
                string line = sr.ReadLine();

                language = (Language)Enum.Parse(typeof(Language), line);

                // Läser in rader från fil till sista raden
                while ((line = sr.ReadLine()) != null)
                {
                    try
                    {
                        //Delar strängarna i 2 vid ;
                        string[] words = line.Split(';');
                        word = new Word(words[0].Trim(), words[1].Trim(), language);
                        wordLibrary.AddWord(word);
                    }
                    catch (Exception)
                    {
                        throw;
                    }

                }
            }

        }

        /// <summary>
        /// Ansvar: Kim Nielsen
        /// Hämtar ut info om språk från fil
        /// </summary>
        /// <param name="fileName"></param>
        /// <returns></returns>
        public static Language LanguageOfFile(string fileName)
        {
            using (StreamReader sr = new StreamReader(fileName, Encoding.Unicode))
            {
                string line = sr.ReadLine();
                return (Language)Enum.Parse(typeof(Language), line);
            }
        }

        /// <summary>
        /// Ansvar: Kim Nielsen
        /// Skriver WordLibrary till fil
        /// </summary>
        /// <param name="language"></param>
        /// <param name="filePath"></param>
        public static void WriteGlossaryToFile(List<Word> wordList, Language language, string fileName)
        {
            using (StreamWriter sw = new StreamWriter(fileName, false, Encoding.Unicode))
            {
                sw.WriteLine(language.ToString());

                foreach (Word word in wordList)
                {
                    sw.WriteLine(word);
                }
            }
        }
    }
}
